package com.vincent.sciinfo.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vincent.sciinfo.pojo.*;
import com.vincent.sciinfo.service.*;
import com.vincent.sciinfo.utils.DateUtil;
import com.vincent.sciinfo.utils.StatusCode;
import com.vincent.sciinfo.utils.StatusMessage;
import com.vincent.sciinfo.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminServer;

    @Autowired
    private PaperService paperServer;

    @Autowired
    private SourceService sourceServer;

    @Autowired
    private LoginCountService loginCountServer;

    @Autowired
    private MessageService messageServer;

    @Autowired
    private PatentService patentServer;

    @Autowired
    private ProjectService projectServer;

    @Autowired
    private TeacherService teacherServer;

    //执行数据库发送消息SQL语句
    private void sendMessage(Message message) {
        messageServer.insertMessage(message);
    }

    //构成消息
    private Message makeUpMessage(String teaId, String product, int ope) {
        Message message = new Message();
        String msg = "你的" + product + "被管理员，执行了：";

        msg = (ope == 1) ? (msg + "通过") : (msg + "下架");
        msg += " 操作。请进入有关页面进行查看。";

        message.setBegin(DateUtil.YMDToNum());
        message.setEnd(DateUtil.GetEndDate());
        message.setFromuser("admin");
        message.setTouser(teaId);
        message.setId(UuidUtil.getUuid(10));
        message.setMessage(msg);
        return message;
    }

    //获得实时日期数据
    private String getDataInfo() {
        int year = Calendar.getInstance().get(Calendar.YEAR);
        int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
        int day = Calendar.getInstance().get(Calendar.DATE);
        String monthDate = String.valueOf(month);
        String dayDate = String.valueOf(day);

        if (month < 10) {
            monthDate = "0" + String.valueOf(month);
        }
        if (day < 10) {
            dayDate = "0" + String.valueOf(day);
        }
        String date = year + monthDate + dayDate;

        return date;
    }

    //给全体教师发消息
    @RequestMapping(value = "/sendMessageToAll")
    public TSMResult sendMessage(@RequestParam("end") String end, @RequestParam("message") String message) throws UnsupportedEncodingException, UnsupportedEncodingException {
        String id = UuidUtil.getUuid(10);
        TSMResult tsmResult = new TSMResult();
        tsmResult.setMessage(StatusMessage.SUCCESS);
        Message message1 = new Message();
        message1.setFromuser("admin");
        message1.setBegin(Integer.parseInt(getDataInfo()));
        message1.setEnd(Integer.parseInt(end));
        message1.setMessage(message);
        message1.setId(id);
        message1.setTouser("allTeacher");
        int data = messageServer.insertMessage(message1);
        tsmResult.setMessage((data == 0) ? "发送失败！" : "发送成功！");
        tsmResult.setData(data);
        return tsmResult;
    }

    //给某位教师发私信
    @RequestMapping(value = "/sendMessageToTeacher")
    public TSMResult sendMessageToTeacher(@RequestParam("teacherId") String teacherid,
                                           @RequestParam("message") String message,
                                           @RequestParam("end") String end) throws UnsupportedEncodingException {
        String id = UuidUtil.getUuid(10);
        TSMResult tsmResult = new TSMResult();
        Message message1 = new Message();
        message1.setFromuser("admin");
        tsmResult.setCode(StatusCode.SUCCESS);
        message1.setBegin(Integer.parseInt(getDataInfo()));
        message1.setEnd(Integer.parseInt(end));
        message1.setMessage(message);
        message1.setId(id);
        message1.setTouser(teacherid);
        int data = messageServer.insertMessage(message1);
        tsmResult.setMessage((data == 0) ? "发送失败！" : "发送成功！");
        tsmResult.setData(data);
        return tsmResult;
    }

    //查询所有状态的论文
    @RequestMapping("/selCheckAndRemovePaper")
    public TSMResult selCheckAndRemovePaper(@RequestParam(value = "pn", defaultValue = "1") Integer pn, @RequestParam Integer pageSize) {
        PageInfo pageInfo = paperServer.selectCheckAndRemovePaper(pn, pageSize);
        return new TSMResult(StatusCode.SUCCESS, StatusMessage.SUCCESS, pageInfo);
    }

    //查询所有状态的专利
    @RequestMapping("/selCheckAndRemovePatent")
    public TSMResult selCheckAndRemovePatent(@RequestParam(value = "pn", defaultValue = "1") Integer pn, @RequestParam Integer pageSize) {
        PageInfo pageInfo = patentServer.selCheckAndRemovePatent(pn, pageSize);
        return new TSMResult(StatusCode.SUCCESS, StatusMessage.SUCCESS, pageInfo);
    }

    //查询所有状态的项目
    @RequestMapping("/selCheckAndRemoveProject")
    public TSMResult selCheckAndRemoveProject(@RequestParam(value = "pn", defaultValue = "1") Integer pn, @RequestParam Integer pageSize) {
        PageInfo pageInfo = projectServer.selectCheckAndRemoveProject(pn, pageSize);
        return new TSMResult(StatusCode.SUCCESS, StatusMessage.SUCCESS, pageInfo);
    }

    //查询科研人员登录系统的次数信息
    @RequestMapping(value = "/showLoginCountData", method = RequestMethod.GET)
    public TSMResult showLoginCountData() {
        List<LoginCount> list = loginCountServer.showLoginCountData();
        return new TSMResult(StatusCode.SUCCESS, StatusMessage.SUCCESS, list);
    }

    //更改论文的状态码
    @RequestMapping("/reUpdatePaper")
    public TSMResult reUpdatePaper(String paperId, int operaCode) {
        TSMResult tsmResult = new TSMResult();
        String message = StatusMessage.SUCCESS;
        Integer data;
        tsmResult.setCode(StatusCode.SUCCESS);
        try {
            paperServer.updatePaperStatus(paperId, operaCode);
        } catch (Exception e) {
            message = "操作失败";
            data = 0;
            tsmResult.setMessage(message);
            tsmResult.setData(data);
            return tsmResult;
        }
        String[] info = paperServer.selectPaperNameById(paperId);
        String str = "论文《" + info[1] + "》";
        sendMessage(makeUpMessage(info[0], str, operaCode));
        tsmResult.setMessage(message);
        tsmResult.setData(1);
        return tsmResult;
    }

    //更改专利的状态码
    @RequestMapping("/updatePatent")
    public TSMResult updatePatent(String patentId, int operaCode) {
        TSMResult tsmResult = new TSMResult();
        try {
            patentServer.updatePatentStatus(patentId, operaCode);
        } catch (Exception e) {
            tsmResult.setData(0);
            tsmResult.setCode(StatusCode.FAILED);
            tsmResult.setMessage(StatusMessage.FAILED);
            return tsmResult;
        }
        String[] info = patentServer.selectPatentNameById(patentId);
        String str = "专利《" + info[1] + "》";
        sendMessage(makeUpMessage(info[0], str, operaCode));
        tsmResult.setData(1);
        tsmResult.setCode(StatusCode.SUCCESS);
        tsmResult.setMessage(StatusMessage.SUCCESS);
        return tsmResult;
    }

    //更改项目状态码
    @RequestMapping("/updateProject")
    public TSMResult updateProject(String projectId, int operaCode) {
        TSMResult tsmResult = new TSMResult();
        try {
            projectServer.updateProjectStatus(projectId, operaCode);
        } catch (Exception e) {
            tsmResult.setCode(StatusCode.SUCCESS);
            tsmResult.setMessage(StatusMessage.SUCCESS);
            tsmResult.setData(0);
        }
        String[] info = projectServer.selectProjectNameById(projectId);
        String str = "项目《" + info[1] + "》";
        sendMessage(makeUpMessage(info[0], str, operaCode));
        tsmResult.setCode(StatusCode.SUCCESS);
        tsmResult.setMessage(StatusMessage.SUCCESS);
        tsmResult.setData(1);
        return tsmResult;
    }

    //查询所有教师
    @RequestMapping("/selAllTeacher")
    public TSMResult selAllTeacher(@RequestParam(value = "pn", defaultValue = "1") Integer pn, @RequestParam Integer pageSize) {
        PageInfo page = teacherServer.selectAllTeacher(pn, pageSize);
        return new TSMResult(StatusCode.SUCCESS, StatusMessage.SUCCESS, page);
    }

    //修改老师状态
    @RequestMapping("/updateTeacher")
    public TSMResult updateTeacher(String teacherId, int operaCode) {
        TSMResult tsmResult = new TSMResult();
        Boolean result = teacherServer.updateTeacher(teacherId, operaCode) == 1;
        tsmResult.setData(new Boolean(result));
        tsmResult.setMessage(StatusMessage.SUCCESS);
        tsmResult.setCode(StatusCode.SUCCESS);
        return tsmResult;
    }

    //发送消息的时候 查询所有没有被注销的教师
    @RequestMapping("/getTeacherListNotLogOut")
    public TSMResult getTeacherListNotLogOut() {
        int status = 1;  //只能给没被注销的教师 发消息
        TSMResult tsmResult = new TSMResult();
        tsmResult.setMessage(StatusMessage.SUCCESS);
        tsmResult.setCode(StatusCode.SUCCESS);
        List<Teacher> teacherList = teacherServer.selectTeacherNotLogOut(status);
        tsmResult.setData(teacherList);
        return tsmResult;
    }

    //查询管理员信息
    @RequestMapping("/getAdminInfo")
    public TSMResult getAdminInfor(@RequestParam("adminId") String adminId) {
        Admin admin = adminServer.selectAdminInfoById(adminId);
        return new TSMResult(StatusCode.SUCCESS, StatusMessage.SUCCESS, admin);
    }

    //管理员更改信息
    @RequestMapping("/adminUpdateInfo")
    public TSMResult adminUpdateInfor(@RequestBody(required = false) Admin admin) {
        TSMResult tsmResult = new TSMResult();
        int data = adminServer.updateAdmin(admin);
        tsmResult.setData(data);
        tsmResult.setMessage(StatusMessage.SUCCESS);
        tsmResult.setCode(StatusCode.SUCCESS);
        return tsmResult;
    }
}
